package com.xueyi.tesla.mapper;

import com.xueyi.game.backend.entity.dto.AddOrUpdateJurisdictionDto;
import com.xueyi.game.backend.entity.po.BaseMenu;
import com.xueyi.game.backend.entity.vo.BaseMenuVo;
import com.xueyi.game.backend.entity.vo.BaseSelectVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author: hxs
 * @createDate: 2022/11/9 16:10
 * @description: 权限管理mapper
 */
public interface BaseJurisdictionMapper {
    /**
     * 获取所有的权限
     *
     * @param authorityType 类型
     * @param menuTableName 菜单
     * @return
     * @author: hxs
     */
    @Select({
            " <script>" +
                    " select * from ${menuTableName} " +
                    " <where> " +
                    " <if test = ' authorityType!=null ' >" +
                    " and authority_type = #{authorityType} " +
                    " </if> " +
                    " <if test = ' isValid!=null ' >" +
                    " and is_valid = 1 " +
                    " </if> " +
                    " </where> " +
                    " </script> "
    })
    List<BaseMenuVo> findAllJurisdiction(@Param("authorityType") Integer authorityType, @Param("menuTableName") String menuTableName,
                                         @Param("isValid") Integer isValid);

    /**
     * 新增权限
     *
     * @param baseMenu      权限对象
     * @param menuTableName 表名称
     * @author hxs
     */
    @Insert({
            " insert into ${menuTableName} ( menu_name,parent_id,menu_icon,menu_path,authority_type,is_valid ,ordered ) " +
                    " values ( #{baseMenu.menuName},#{baseMenu.parentId},#{baseMenu.menuIcon},#{baseMenu.menuPath}" +
                    " ,#{baseMenu.authorityType},#{baseMenu.isValid},#{baseMenu.ordered} ) "
    })
    void addJurisdiction(@Param("baseMenu") BaseMenu baseMenu, @Param("menuTableName") String menuTableName);

    /**
     * 修改权限
     *
     * @param addOrUpdateJurisdictionReq
     * @return
     * @author hxs
     */
    @Update({
            " update ${menuTableName} set  menu_name = #{item.menuName} , parent_id = #{item.parentId} , menu_icon = #{item.menuIcon} , menu_path = #{item.menuPath} ," +
                    " authority_type = #{item.authorityType} , ordered = #{item.ordered} where id = #{item.id}  "
    })
    void updateJurisdiction(@Param("item") AddOrUpdateJurisdictionDto addOrUpdateJurisdictionReq, @Param("menuTableName") String menuTableName);

    /**
     * 获取所有的顶级菜单
     *
     * @return
     * @author hxs
     */
    @Select({
            " select id value , menu_name label from ${menuTableName} where is_valid = 1 and parent_id = 0 and authority_type = #{type} "
    })
    List<BaseSelectVo> findAllFatherMenu(@Param("type") Integer authorityType, @Param("menuTableName") String menuTableName);

    /**
     * 删除权限
     *
     * @param id            权限id
     * @param menuTableName 表名
     * @author hxs
     */
    @Update({
            " update ${menuTableName} set is_valid = #{isValid} where id = #{id} "
    })
    void deleteJurisdictionById(@Param("id") Integer id, @Param("isValid") Integer isValid, @Param("menuTableName") String menuTableName);

    /**
     * 物理删除权限
     *
     * @param menuId 权限id
     * @author hxs
     */
    @Delete({
            " delete from  ${table}  where id = #{menuId}  "
    })
    void removeMenuById(@Param("menuId") Integer menuId, @Param("table") String table);

    /**
     * 通过权限id查询关联的角色id
     *
     * @param id                 权限id
     * @param authorityTableName
     */
    @Select({
            " select group_concat(role_id) role_id from ${authorityTableName} where menu_id = #{id}  "
    })
    String findRolesByMenuId(@Param("id") Integer id, @Param("authorityTableName") String authorityTableName);
}
